<?php

/**
 * Handle Login, Logout, Register
 * @author luckymancvp  <luckymancvp@gmail.com>
 * @version 1.0
 */

class SiteController extends Controller {

	const SERVER_KEY = 'AIzaSyCaHSOBwdT3PT7Hx1MBM_j9EhplDK5klLY';
	const GCM_SERVER = 'https://android.googleapis.com/gcm/send';
	
    /**
     * Register new user
     * @param string $username
     * @param string $password
     * @param string $email 
     * @param string $full_name 
     * @return JSON register status
     */
    public function actionSignup() {
        // Get param
        $username  = Yii::app()->request->getParam('username');
        $password  = md5(Yii::app()->request->getParam('$password'));
        $email     = Yii::app()->request->getParam('email');
        $full_name = Yii::app()->request->getParam('full_name');
        
        // Get value for model
        $model = new Users();
        $model->username  = $username;
        $model->password  = md5($password);
        $model->email     = $email;
        $model->full_name = $full_name;
        $model->avatar    = 'default_avatar.png';
        $model->created_date = new CDbExpression('NOW()');
        
        // init value
        $result = array('result'=>1);
        if (!$model->save()){
            $result['result'] = 0;
            $result['errors'] = $model->errors;
        }
        $this->end($result);
    }
    
    /**
     * Login User
     * @param string $username
     * @param string $password
     * @return JSON Login status
     */
    public function actionSignin() {
        // Get vaule for model
        $model = new LoginForm();
        $model->username = Yii::app()->request->getParam('username');
        $model->password = Yii::app()->request->getParam('password');
        // init value
        $result = array('result'=>1);
        if ( !$model->validate() || !$model->login()){
            $result['result'] = 0;
            $result['errors'] = $model->errors;
        }else{
            $user = Users::model()->findByPk(Yii::app()->user->id);
            $result['user'] = array(
                'user_id'  => $user->id,
                'username' => $user->username,
                'full_name'=> $user->full_name,
                'avatar'   => Html::avatarUrl($user->avatar),
                'status'   => '-1',
            );
        }
        $this->end($result);
    }
    
    public function actionAutologin() {
        $user = Users::model()->findByPk(Yii::app()->user->id);
        $result = array('error'=>'Your session is expired');
        if ($user) 
            $result= array(
                'user_id'  => $user->id,
                'username' => $user->username,
                'full_name'=> $user->full_name,
                'avatar'   => Html::avatarUrl($user->avatar),
                'status'   => '-1',
            );
        $this->end($result);
    }
    
    public function actionSigout() {
        Yii::app()->user->logout();
    }
    
    public function actionGetCurrentUserID() {
        if ( Yii::app()->user->id) die ( Yii::app()->user->id);
        else die("Must login first");
    }
}